package com.yutian.endecryption.mock.service;

import com.yutian.endecryption.aes.util.SymmetricEncoder;
import com.yutian.endecryption.config.Config;
import com.yutian.endecryption.mock.mapper.SubGroupDetailMapper;
import com.yutian.endecryption.mock.pojo.SubGroupDetail;

/**
 * 考生分组明细 service
 *
 * @author zhang peng yang ming
 * @create 2017-08-01 12:06
 */
public class SubGroupDetailService extends BaseService<SubGroupDetailMapper> {
    /**
     * 根据身份证号码检索 考生分组详情
     *
     * @param sfzmhm
     * @return
     */
    public SubGroupDetail retrieveSubGroupDetail (String sfzmhm) {
        SubGroupDetail detail = mapper(SubGroupDetailMapper.class).
                retrieveSubGroupDetail(SymmetricEncoder.AESEncode(Config.KEY, sfzmhm));
        sessionClose();
        if (detail != null) {
            // 对应字段解密
            detail.setSfzmhm(SymmetricEncoder.AESDecode(Config.KEY, detail.getSfzmhm()));
            detail.setXm(SymmetricEncoder.AESDecode(Config.KEY, detail.getXm()));
        }

        return detail;
    }

    /**
     * 更新考生分组详情
     *
     * @param detail
     * @return
     */
    public int updateSubGroupDetail (SubGroupDetail detail) {
        int r = 0;
        // 对应字段加密
        detail.setSfzmhm(SymmetricEncoder.AESEncode(Config.KEY, detail.getSfzmhm()));
        detail.setXm(SymmetricEncoder.AESEncode(Config.KEY, detail.getXm()));
        r = mapper(SubGroupDetailMapper.class).updateSubGroupDetail(detail);
        sessionClose();

        return r;
    }

    /**
     * 插入考生分组详情
     *
     * @param detail
     * @return
     */
    public int createSubGroupDetail (SubGroupDetail detail) {
        int r = 0;
        SubGroupDetail subGroupDetail = retrieveSubGroupDetail(detail.getSfzmhm());
        if (subGroupDetail != null) {
            detail.setId(subGroupDetail.getId());
            r = updateSubGroupDetail(detail);
        } else {
            // 对应字段加密
            detail.setSfzmhm(SymmetricEncoder.AESEncode(Config.KEY, detail.getSfzmhm()));
            detail.setXm(SymmetricEncoder.AESEncode(Config.KEY, detail.getXm()));
            r = mapper(SubGroupDetailMapper.class).createSubGroupDetail(detail);
            sessionClose();
        }

        return r;
    }
}
